package com.rongshu.utils;

import android.text.TextUtils;

import com.utils.LogUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public final static String YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 日期转换:
     * 1、今天/昨天 转换为 昨天/今天 14:52
     * 2、本年昨天以前转换为 12月13日
     * 3、本年以前转换为 2018年12月13日
     *
     * @param msg 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     */
    public static String ConvertDate(String msg) {
        String result = "";
        if (TextUtils.isEmpty(msg)) {
            return result;
        }
        if (IsCurrentYear(msg)) {
            try {
                if (IsToday(msg) || IsYesterday(msg)) {
                    result = ConvertDayTime(msg);
                } else {
                    result = ConvertMonthDay(msg);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            result = ConvertYearMonthDay(msg);
        }
        return result;
    }


    /**
     * 格式日期月日，不是当前年，显示年份
     *
     * @param msg
     * @return
     */
    public static String ConvertMonth(String msg) {
        String result = "";
        if (TextUtils.isEmpty(msg)) {
            return result;
        }
        if (IsCurrentYear(msg)) {
            result = ConvertMonthDay(msg);
        } else {
            result = ConvertYearMonthDay(msg);
        }
        return result;
    }

    /**
     * 日期转换为：2018年12月13日 格式
     *
     * @param msg 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     */
    public static String ConvertYearMonthDay(String msg) {
        Calendar cal = Calendar.getInstance();
        Date date = null;
        try {
            date = getDateFormat(YYYY_MM_DD_HH_MM_SS).parse(msg);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        return year + "年" + month + "月" + day + "日";
    }

    /**
     * 日期转换为：12月13日 格式
     *
     * @param msg 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     */
    public static String ConvertMonthDay(String msg) {
        String monthMsg, dayMsg;
        Calendar cal = Calendar.getInstance();
        Date date = null;
        try {
            date = getDateFormat(YYYY_MM_DD_HH_MM_SS).parse(msg);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.setTime(date);
        //月份是从0开始。
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        monthMsg = month < 10 ? "0" : "";
        dayMsg = day < 10 ? "0" : "";
        return monthMsg + month + "月" + dayMsg + day + "日";
    }

    /**
     * 日期转换为：昨天/今天 14:52 格式
     *
     * @param msg 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     */
    public static String ConvertDayTime(String msg) {
        String day = "";
        String minuteStr = "";
        Calendar cal = Calendar.getInstance();
        Date date = null;
        //当前时间 new
        Date current_data = new Date();
        try {
            date = getDateFormat(YYYY_MM_DD_HH_MM_SS).parse(msg);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.setTime(date);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        //二者时间差 new
        long differ = (current_data.getTime() - date.getTime()) / 1000;
        if (differ < 60) {//小于60s
            return "刚刚";
        } else if (differ < 60 * 60) {
            return String.valueOf(Math.round(differ / 60)) + "分钟前";
        }

        if (minute < 10) {
            minuteStr = "0" + minute;
        } else {
            minuteStr = "" + minute;
        }

        try {
            if (IsToday(msg)) {
                day = "今天";
            } else if (IsYesterday(msg)) {
                day = "昨天";
            }
        } catch (ParseException e) {
            e.printStackTrace();
            LogUtil.i(e.toString());
        }
        return day + " " + hour + ":" + minuteStr;
    }


    /**
     * 判断是否为今年(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsCurrentYear(String day) {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = null;
        try {
            date = getDateFormat(YYYY_MM_DD_HH_MM_SS).parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsToday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat(YYYY_MM_DD_HH_MM_SS).parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);
            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为昨天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsYesterday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat(YYYY_MM_DD_HH_MM_SS).parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取前一天的时间
     *
     * @param timeType
     * @return
     */
    public static String getYesterday(String timeType) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1); //得到前一天
        Date date = calendar.getTime();

        return getDateFormat(timeType).format(date);
    }

    /**
     * 获取当天的时间
     *
     * @param timeType
     * @return
     */
    public static String getToday(String timeType) {

        return getDateFormat(timeType).format(new Date());
    }

    /**
     * 获取当天是周几
     *
     * @return
     */
    public static String getTodayWeek() {
        Calendar calendar = Calendar.getInstance();//获取实例
        String curWeek = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        switch (curWeek) {
            case "1":
                curWeek = "周日";
                break;
            case "2":
                curWeek = "周一";
                break;
            case "3":
                curWeek = "周二";
                break;
            case "4":
                curWeek = "周三";
                break;
            case "5":
                curWeek = "周四";
                break;
            case "6":
                curWeek = "周五";
                break;
            case "7":
                curWeek = "周六";
                break;

        }
        return curWeek;
    }


    public static SimpleDateFormat getDateFormat(String timeType) {
        DateLocal.set(new SimpleDateFormat(timeType, Locale.CHINA));
        return DateLocal.get();
    }

    /**
     * 日期格式化 成 字符串
     *
     * @param format
     * @param date
     * @return
     */
    public static String formatDate(String format, Date date) {
        if (null != date) {
            SimpleDateFormat formater = new SimpleDateFormat(format);
            return formater.format(date);
        }
        return "";
    }

    /**
     * 解析 日期字符串为Date
     *
     * @param format
     * @param dateString
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String format, String dateString) {
        if (!TextUtils.isEmpty(dateString)) {
            SimpleDateFormat formater = new SimpleDateFormat(format);
            try {
                return formater.parse(dateString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return new Date();

    }

    /**
     * 根据参数 生成date
     *
     * @param year
     * @param monthOfYear 0~11
     * @return
     */
    public Date getDate(int year, int monthOfYear, int dayOfMonth) {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.YEAR, year);
        ca.set(Calendar.MONTH, monthOfYear);
        ca.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        return ca.getTime();
    }

    /**
     * 根据时间参数返回日
     */
    public static String getDay(String time) {
        String day = "";
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int index = c.get(Calendar.DAY_OF_MONTH);
        if (index < 10) {
            day = "0" + index;
        } else {
            day = "" + index;
        }
        return day;
    }


    /**
     * 根据当前日期获得是星期几
     */
    public static String getWeek(String time) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int wek = c.get(Calendar.DAY_OF_WEEK);

        if (wek == 1) {
            Week += "周日";
        }
        if (wek == 2) {
            Week += "周一";
        }
        if (wek == 3) {
            Week += "周二";
        }
        if (wek == 4) {
            Week += "周三";
        }
        if (wek == 5) {
            Week += "周四";
        }
        if (wek == 6) {
            Week += "周五";
        }
        if (wek == 7) {
            Week += "周六";
        }
        return Week;
    }
}
